package xyz.comfyz.web.commons.security.access.basic;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import xyz.comfyz.web.commons.security.access.SecurityFilter;
import xyz.comfyz.web.commons.security.cache.CacheStorager;
import xyz.comfyz.web.commons.security.model.TokenConfig;
import xyz.comfyz.web.commons.security.provider.TokenProvider;
import xyz.comfyz.web.commons.security.provider.token.TokenReader;

/**
 * @author :    comfy
 * @date :      2018-05-12 16:04:49
 * @since :     1.8
 * <p>
 * 配置执行生效
 */
@Component
public final class SecurityWorker {

    @SuppressWarnings("unchecked")
    @Autowired
    public SecurityWorker(WebSecurityConfigAdapter webSecurityConfigAdapter, HttpSecurity httpSecurity,
                          SecurityFilter securityFilter, SecurityMetadataSource securityMetadataSource,
                          TokenProvider tokenProvider) {
        webSecurityConfigAdapter.config(httpSecurity);
        securityFilter.setMatcher(httpSecurity.filterPath());
        securityMetadataSource.authorized(httpSecurity.authorizations());
        securityMetadataSource.exclude(httpSecurity.exclusions());

        final TokenConfig tokenConfig = httpSecurity.tokenConfig();
        tokenProvider.config(tokenConfig);
        if (httpSecurity.enableCookie())
            tokenProvider.enableCookie(httpSecurity.enableLocalCookie());

        tokenProvider.add(httpSecurity.tokenCache().toArray(new CacheStorager[0]));
        tokenProvider.add(httpSecurity.tokenReader().toArray(new TokenReader[0]));
    }

}
